﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BO.Admin;
using System.Data;

namespace ProjectQuaDep.Admin.Products
{
    public partial class DownloadProduct : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ShowAll();
            }
        }

        protected void ShowAll()
        {
            string ProductID = GetRequest.ProductID;
            string search = txtSearch.Text.Trim();
            DataTable dt = ProductsBO.GetAllListDownloadBO(ProductID, search);
            gvShow.DataSource = dt;
            gvShow.DataBind();
        }

        protected void cmdUpdate_Click(object sender, EventArgs e)
        {
            string ProductID = GetRequest.ProductID;
            if (ProductID == "")
            {
                Response.Write("<script>alert('Không thêm được dữ liệu!')</script>");
                return;
            }
            if (cmdUpdate.Text == "Thêm Mới")
            {
                //them moi
                string kq = ProductsBO.AddDownloadProduct(ProductID, txtLinkDownload.Text, "", BO.Utils.getOneSelect(ckSelect), "0", txtTitle.Text);
                if (kq == "0")
                {
                    lblMs.Text = "Xảy ra lỗi khi thêm mới bạn hãy xem lại!";
                }
                else
                {
                    lblMs.Text = "";
                    ShowAll();
                    pnUpdate.Visible = false;
                }
            }
            else
            {
                //sua chua
                string kq = ProductsBO.EditDownloadProduct(ProductID, txtLinkDownload.Text, "", BO.Utils.getOneSelect(ckSelect), "0", txtTitle.Text,lblID.Text);
                if (kq == "0")
                {
                    lblMs.Text = "Xảy ra lỗi khi cập nhật bạn hãy xem lại!";
                }
                else
                {
                    lblMs.Text = "";
                    ShowAll();
                    pnUpdate.Visible = false;
                }
            }
        }

        protected void cmdHide_Click(object sender, EventArgs e)
        {
            pnUpdate.Visible = false;
        }

        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            ShowAll();
        }

        protected void cmdAdd_Click(object sender, ImageClickEventArgs e)
        {
            pnUpdate.Visible = true;
            cmdUpdate.Text = "Thêm Mới";

        }

        protected void gvShow_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvShow.PageIndex = e.NewPageIndex;
            lblPage.Text = "Trang: " + (gvShow.PageIndex + 1).ToString();
            ShowAll();
        }

        protected void gvShow_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string cmdName = e.CommandName.ToString();
            string ID = e.CommandArgument.ToString();
            if (cmdName == "cmdEdit")
            {
                DataTable dt = ProductsBO.GetInfomationDownloadPorductBO(ID);
                if (dt.Rows.Count > 0)
                {
                    cmdUpdate.Text = "Sửa Chữa";
                    pnUpdate.Visible = true;
                    lblID.Text = ID;
                    txtTitle.Text = dt.Rows[0][BO.Const.tbDownloadProduct.Title].ToString();
                    txtLinkDownload.Text = dt.Rows[0][BO.Const.tbDownloadProduct.LinkDownload].ToString();
                    BO.Utils.setOneSelect(ckSelect, dt.Rows[0][BO.Const.tbDownloadProduct.Status].ToString());
                }
            }
        }
    }
}